package copyengine.utils.sequence
{

    public class CESequenceHolder
    {
        private var mAllSequenceVector:Vector.<CESequenceBasic>;
        private var mOnAllSequenceExecuteFinishCallback:Function;
        private var mCurrentSequence:CESequenceBasic;

        public function CESequenceHolder() {
            mAllSequenceVector = new Vector.<CESequenceBasic>();
        }

        public function addSequence(_sequence:CESequenceBasic):void {
            _sequence.finishCallback = onCurrentSequenceExecuteFinish;
            mAllSequenceVector.push(_sequence);
        }

        public function execute(_finishCallback:Function):void {
            mOnAllSequenceExecuteFinishCallback = _finishCallback;
            executeNextSequenceOrFinishAll();
        }

        public function dispose():void {
            mAllSequenceVector.length = 0;
            mOnAllSequenceExecuteFinishCallback = null;
        }

        //========================//
        //==Private  Function
        //=========================//

        private function onCurrentSequenceExecuteFinish():void {
            executeNextSequenceOrFinishAll();
        }

        private function executeNextSequenceOrFinishAll():void {
            if (mAllSequenceVector.length > 0) {
                mCurrentSequence = mAllSequenceVector.shift();
                mCurrentSequence.execute();
            } else {
                mOnAllSequenceExecuteFinishCallback && mOnAllSequenceExecuteFinishCallback();
            }
        }

    }

}
